﻿using System;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;
using YArchitech.Revit.MEP.Public;

namespace YArchitech.Revit.MEP.Command
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class ConnectWire : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Document document = activeUIDocument.ActiveView.Document;
			WireParamForm wireParamForm = null;
			WireParamForm wireParamForm2;
			wireParamForm = (wireParamForm2 = new WireParamForm(document, EnumFormMode.Non_mode));
			try
			{
				wireParamForm.Show(WindowHelper.HWndRevit);
				WindowHelper.SendMessageToWndRevit();
                if (wireParamForm.ResultDlg == Result.Failed)
				{
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				for (;;)
				{
					Transaction transaction = new Transaction(document, "hy");
					transaction.Start();
					try
					{
						if (wireParamForm.isClose)
						{
							transaction.Commit();
							break;
						}
						EquipmentConnect.CreateWireBySelection(activeUIDocument, wireParamForm);
					}
					catch (Exception)
					{
						if (EquipmentConnect.isFirstEquipment)
						{
							wireParamForm.Close();
							transaction.Commit();
							break;
						}
						if (wireParamForm.isClose)
						{
							transaction.Commit();
							break;
						}
						transaction.Commit();
					}
				}
			}
			finally
			{
				if (wireParamForm2 != null)
				{
					((IDisposable)wireParamForm2).Dispose();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
